package com.yc.fruitmarket.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yc.fruitmarket.biz.cartBiz;
import com.yc.fruitmarket.biz.impl.cartBizImpl;

@WebServlet("/carInfoController/*")
public class cartController extends BaseServlet {
	private cartBiz cb = new cartBizImpl();
	public void getCartCount(HttpServletRequest req, HttpServletResponse resp) {
		String uid = req.getParameter("uid");
		if(!"".equals(uid)) {
			int count = cb.getCartCount(uid);
			this.send(resp, count);
		}else {
			return;
		}
	}
	//添加购物车
	public void addCart(HttpServletRequest req, HttpServletResponse resp) {
		String uid = req.getParameter("uid");
		String pid = req.getParameter("pid");
		String num = req.getParameter("num");
		String inventory = req.getParameter("inventory");
		if("".equals(uid) || "".equals(pid)) {
			this.send(resp, -1);
			return;
		}else {
			int result = cb.addCart(uid, pid, num, inventory);
			this.send(resp, result);
		}
	}
	//根据uid查购物车信息
	public void getCartsByUid(HttpServletRequest req, HttpServletResponse resp) {
		String uid = req.getParameter("uid");
		if("".equals(uid) || uid == null) {
			return;
		}
		List<Map<String, String>> list = cb.getCartsByUid(uid);
		if(list.size() > 0) {
			for(int i = 0; i < list.size(); i++) {
		 		list.get(i).put("checked", "true");
		 	}
		 	this.send(resp, list);
		}else {
			return;
		}
	}
	//根据cid修改购物车数量
	public void changeNum(HttpServletRequest req, HttpServletResponse resp) {
		String cid = req.getParameter("cid");
		String cartnum = req.getParameter("cartnum");
		String inventory = req.getParameter("inventory");
		System.out.println(cid + inventory + "参数");
		int reslut = cb.changeNum(cid,cartnum, inventory);
		this.send(resp, reslut);
	}
	//根据cid删除购物车信息
	public void deleteCart(HttpServletRequest req, HttpServletResponse resp) {
		String cid = req.getParameter("cid");
		int reslut = cb.deleteCart(cid);
		this.send(resp, reslut);
	}
	//根据cids查找购物车信息
	public void findByCids(HttpServletRequest req, HttpServletResponse resp) {
		String cids = req.getParameter("cids");
		if("".equals(cids) || cids == null) {
			return;
		}else {
			List<Map<String, String>> list = cb.findByCids(cids);
			this.send(resp, list);
		}
	}
	//购买成功删除购物车
	public void deleteCartP(HttpServletRequest req, HttpServletResponse resp) {
		String uid = req.getParameter("uid");
		String cids = req.getParameter("cids");
		cb.deleteCartP(uid, cids);
		this.send(resp, 1);
	}
}
